﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Serialization;
using Engine.Dtd.Schema;
using System.Diagnostics;

namespace Engine.Sgml {
    /// <summary>
    /// Represent errors that occur during parsing the SGML document.
    /// </summary>
    [Serializable]
    public class SgmlException : Exception {
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private string context;

        /// <summary>
        /// Instantiates a new instance of <see cref="Engine.Sgml.SgmlException"/> with no specific error information.
        /// </summary>
        public SgmlException() {
            }

        /// <summary>
        /// Instantiates a new instance of <see cref="Engine.Sgml.SgmlException"/> with an error message describing the problem.
        /// </summary>
        /// <param name="message">A message describing the error that occurred</param>
        public SgmlException(string message)
            : base(message) {
            }

        /// <summary>
        /// Instantiates a new instance of <see cref="Engine.Sgml.SgmlException"/> with an error message describing the problem.
        /// </summary>
        /// <param name="message">A message describing the error that occurred</param>
        /// <param name="context">A string that contains the contextual information about the source on which the error occurred.</param>
        public SgmlException(string message, string context)
            : base(message) {
            this.context = context;
            }

        /// <summary>
        /// Instantiates a new instance of <see cref="Engine.Sgml.SgmlException"/> with an error message describing the problem.
        /// </summary>
        /// <param name="message">A message describing the error that occurred</param>
        /// <param name="innerException">The original exception that caused the problem.</param>
        public SgmlException(string message, Exception innerException)
            : base(message, innerException) {
            }

        /// <summary>
        /// Initializes a new instance of the <see cref="Engine.Sgml.SgmlException"/> class with serialized data. 
        /// </summary>
        /// <param name="streamInfo">The object that holds the serialized object data.</param>
        /// <param name="context">The contextual information about the source or destination.</param>
        protected SgmlException(SerializationInfo streamInfo, StreamingContext context)
            : base(streamInfo, context) {
            if (streamInfo != null)
                this.context = streamInfo.GetString("entityContext");
            }

        /// <summary>
        /// Gets the contextual information about the source on which the error occurred.
        /// </summary>
        public string Context {
            get {
                return context;
                }
            }

        /// <summary>
        /// Populates a SerializationInfo with the data needed to serialize the exception.
        /// </summary>
        /// <param name="info">The <see cref="SerializationInfo"/> to populate with data. </param>
        /// <param name="context">The destination (see <see cref="StreamingContext"/>) for this serialization.</param>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context) {
            Guard.ArgumentNotNull(info, "info");
            info.AddValue("entityContext", context);
            base.GetObjectData(info, context);
            }
        }
    }
